//
//  HMComposeTextView.swift
//  CZBKWB31
//
//  Created by Linkon Y on 16/8/17.
//  Copyright © 2016年 itcast. All rights reserved.
//

import UIKit

class HMComposeTextView: UITextView {
    //重写属性
    override var font: UIFont?{
        didSet{
        placeholderLabel.font = font
        }
    }
    
    override var textColor: UIColor?{
        didSet{
            placeholderLabel.textColor = textColor
        }
    }
     var placeholder: String?{
        didSet{
            placeholderLabel.text = placeholder
        }
    }
    
    override init(frame: CGRect, textContainer: NSTextContainer?) {
        super.init(frame: frame, textContainer: textContainer)
        setupUI()
        
    }
    
    required init?(coder aDecoder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    private func setupUI(){
    
    backgroundColor = UIColor.whiteColor()
         // MARK: - 添加控件
        addSubview(placeholderLabel)
         // MARK: - 约束
        placeholderLabel.snp_makeConstraints { (make) in
            make.top.equalTo(self).offset(8)
            make.leading.equalTo(self).offset(5)
            make.width.equalTo(SCREENW - 10)
        }
        //监听系统通知
        NSNotificationCenter.defaultCenter().addObserver(self, selector: "TextDidChangeDidChange", name: UITextViewTextDidChangeNotification, object: nil)

    }
    
    func TextDidChangeDidChange(){
    placeholderLabel.hidden = self.hasText()
    }
    
    
    // MARK: - 懒加载控件
    private lazy var placeholderLabel: UILabel = {
        let lab = UILabel()
        lab.numberOfLines = 0
        return lab
    }()
    //析构函数
    deinit{
    NSNotificationCenter.defaultCenter().removeObserver(self)
    
    }
    
}
